package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.NavDao;
import com.cn.wanxi.dao.impl.NavImpl;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.service.INavService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NavServiceImpl implements INavService {
    /**
     * 判断缓存里面是否有数据，
     * 有，取缓存
     * 没有取数据库
     *
     * @return
     */
    @Override
    public List<NavModel> getNavModelList() {
        NavDao navDao = new NavImpl();
//        String ,String
//                Object  Objecj
        List<NavModel> list = new ArrayList<>();//Map
        Jedis jedis = new Jedis();
        boolean isHave = jedis.exists("");
        if (isHave) {
            long length = jedis.llen("nav_title");
            for (int i = 0; i < length; i++) {
                NavModel model = new NavModel();
                model.setTitle(jedis.lindex("nav_title", i));
                model.setHref(jedis.lindex("nav_href", i));
                list.add(model);
            }
        } else {
            list = navDao.findAll();
            for (int i = 0; i < list.size(); i++) {
                jedis.rpush("nav_title", list.get(i).getTitle());
                jedis.expire("nav_title", 60);
                jedis.rpush("nav_href", list.get(i).getHref());
            }

        }
        return list;
    }

}
